<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'tenant_id',
        'phone',
        'nickname',
        'realname',
        'avatar_url',
        'weapp_openid',
        'gender',
        'is_member',
        'member_expired_time',
        'member_remain_times',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function coach()
    {
        return $this->hasOne(Coach::class);
    }

    public function coachOrders()
    {
        return $this->hasMany(CoachOrder::class);
    }

    public function groupCourseOrders()
    {
        return $this->hasMany(GroupCourseOrder::class);
    }

    public function todayGroupCourseOrderCount()
    {
        return $this->groupCourseOrders()
            ->where(['is_paid' => true])
            ->where('status', '!=', GroupCourseOrder::STATUS_CANCELED)
            ->whereDate('paid_time', Carbon::today()->toDateString())
            ->count();
    }
}
